screentable:
i=0
rept 128
	.dw testmap+i*192
	i = i + 1
endr


objcolrow:
	pha
dropobjcolrow:
	ldx VM_O
	lda OBJXhigh,x  ; col
	pha
	lda OBJYhigh,x  ; row
	vmnext


macro maploc pointer
	ldx #$03        ;>#TILEMAP_BUF
	stx pointer+1
	tax
	lda mul16table,x
	sta pointer
	pla
	clc
	adc pointer
	sta pointer
endm

mapfetch:  ; col row -- n
	maploc VM_B
	ldx #0
	lda (VM_B,x)
	vmnext

tilecoll:  ; n -- n
	tax
	lda bg1metatiles_coll,x
	vmnext

amaploc:   ; col row -- A=addr
	maploc VM_A
	vmnext_pla



dfetchplus:	colon
	.dw fetchplus,fetchplus,swap,exit


update_tilemap_row:  ; A=src  --   A=src+16
	colon
		.dw lit,16
		--for .dw for
			.dw twolit, bg1metatiles, zero, fetchplus, twomul, dplus, ex_dfetch, ppustore, ppustore
		mnext --for
	.dw exit

update_tilemap_row2:  ; A=src   --   A=src+16
	colon
		.dw lit,16
		--for .dw for
			.dw twolit, bg1metatiles+32, zero, fetchplus, twomul, dplus, ex_dfetch, ppustore, ppustore
		mnext --for
	.dw exit


update_screen:
	colon
	.dw forceblank
	.dw twolit,$2000,ppuaddress
	.dw twolit,TILEMAP_BUF
	.dw lit,12
	-for .dw for
		.dw twodup, astore
		.dw update_tilemap_row
		.dw twodup, astore
		.dw update_tilemap_row2
		.dw lit,16,plus
	mnext -for
	.dw twodrop

	.dw twolit,$23c0,ppuaddress

	; need to do the attributes...
	;  for now just clear em
	dw lit,64
	-for .dw for
		.dw lit,0,ppustore
	mnext -for

	.dw exit

load_screen:  ; n --
	colon
	.dw lit, CURRENT_BANK, fetch, swap
	.dw lit, 6, bankswitch
	.dw twomul, zero, swap, twolit, screentable, dplus, astore, dfetchplus, astore
	.dw twolit,TILEMAP_BUF,lit,192,move
	.dw bankswitch
	.dw update_screen
	.dw exit
